feat: add selectConfig for config files that can't use top-level await#19
Closed
rqbazan wants to merge 1 commit into
Closed
feat: add selectConfig for config files that can't use top-level await#19rqbazan wants to merge 1 commit into
rqbazan wants to merge 1 commit into
Conversation
Contributor
Preview releaseLatest commit: Some packages have been released:
Note Use the PR number as tag to install any package. For instance: |
f37ea50 to
2d0a24f
Compare
`selectConfig(configs)` is a synchronous, runtime-agnostic counterpart to `loadConfig`. It pairs with static `import`s and picks the entry matching `envName()`, so config files that tooling loads via `require()` or bundles to CJS — where top-level await is rejected (ERR_REQUIRE_ASYNC_MODULE) — can wire up env without async. - core: `selectConfig` + export from the main entry - tests: runtime tests + type tests - example: `config-cjs` (Node 22.22.3) asserting the require() failure mode and the selectConfig fix - docs: loadConfig guide section + core API reference row - changeset: minor Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2d0a24f to
41bddaa
Compare
Member
Author
|
Superseded by a sync |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
loadConfigis async (it uses dynamicimport(), which is irreducibly asynchronous). That's fine in app code, but it breaks in config files that tooling loads synchronously — files pulled in viarequire()or bundled to CJS, where a top-levelawaitis rejected (ERR_REQUIRE_ASYNC_MODULE, or the esbuild "top-level await is not supported with the cjs output format"). There's no runtime-agnostic way to makeloadConfigsync (a sync module load meansrequire(), which only exists on CJS-capable runtimes and can't load.tswithout a transpile hook).What
selectConfig(configs)— a synchronous, runtime-agnostic counterpart. Pair it with staticimports so the runtime/bundler resolves and transpiles each config at parse time; it picks the entry matchingenvName()and throws when there's none (the map is explicit, so a miss is almost always a typo).Changes
selectConfig(package/src/lib/select-config.ts) + export from the main entry.select-config.test.ts) + type tests inlib.test-d.ts(return type isT, neverT | undefined; pipes intodefineEnv).examples/config-cjs/(pinned to Node 22.22.3) — anode:testthatrequire()s both aselectConfigconfig (loads ✓) and aloadConfig+ top-level await config (throwsERR_REQUIRE_ASYNC_MODULE✓). Registered in the rootconfig_roots.loadConfigguide + a row in the core API reference + example table.Verification
examples/config-cjstest:e2e: both assertions pass.rr check(package + docsite) + examplecheck: clean.🤖 Generated with Claude Code